frappe.query_reports["Disclosure Details CN"] = {
    filters: [
        {
            fieldname: "company",
            label: __("公司"),
            fieldtype: "Link",
            options: "Company",
            default: frappe.defaults.get_user_default("Company"),
            reqd: 1,
            on_change: function() {
                frappe.query_report.set_filter_value("parent_account", "");
            }
        },
        {
            fieldname: "fiscal_year",
            label: __("会计年度"),
            fieldtype: "Link",
            options: "Fiscal Year",
            // 核心修改：优先取用户默认，若无则默认当前年份
            default: function() {
                const userDefault = frappe.defaults.get_user_default("fiscal_year");
                if (userDefault) {
                    return userDefault;
                }
                // 获取当前年份作为默认值
                const currentYear = new Date().getFullYear().toString();
                return currentYear;
            },
            reqd: 1,
            on_change: function(query_report) {
                var fiscal_year = query_report.get_values().fiscal_year;
                if (fiscal_year) {
                    frappe.model.with_doc("Fiscal Year", fiscal_year, function() {
                        frappe.query_report.set_filter_value("from_month", "一月");
                        frappe.query_report.set_filter_value("to_month", "十二月");
                    });
                }
            }
        },
        {
            fieldname: "period",
            label: __("期间类型"),
            fieldtype: "Select",
            options: ["月度", "季度", "半年度", "年度"],
            default: "月度"
        },
        {
            fieldname: "from_month",
            label: __("起始月份"),
            fieldtype: "Select",
            options: ["一月", "二月", "三月", "四月", "五月", "六月", 
                      "七月", "八月", "九月", "十月", "十一月", "十二月"],
            default: "一月",
            reqd: 1
        },
        {
            fieldname: "to_month",
            label: __("结束月份"),
            fieldtype: "Select",
            options: ["一月", "二月", "三月", "四月", "五月", "六月", 
                      "七月", "八月", "九月", "十月", "十一月", "十二月"],
            default: "十二月",
            reqd: 1
        },
        {
            fieldname: "parent_account",
            label: __("按上级科目筛选"),
            fieldtype: "Link",
            options: "Account",
            get_query: function() {
                var company = frappe.query_report.get_filter_value("company");
                return {
                    filters: {
                        "company": company,
                        "is_group": 1
                    }
                };
            }
        },
        {
            fieldname: "show_zero_values",
            label: __("显示零值行"),
            fieldtype: "Check",
            default: 0,  // 默认不勾选
            on_change: function(query_report) {
                // 勾选状态变化时重新加载报表
                query_report.refresh();
            }
        }
    ],
    tree: true,
    name_field: "account",
    parent_field: "parent_account",
    initial_depth: 3,
    formatter: function(value, row, column, data, default_formatter) {
        value = default_formatter(value, row, column, data);
        
        if (data && data.is_group) {
            value = $(`<span>${value}</span>`).css("font-weight", "bold").prop("outerHTML");
        }
        
        if (["opening_debit", "opening_credit", "debit", "credit", "closing_debit", "closing_credit"].includes(column.fieldname)) {
            if (flt(value) === 0 && !frappe.query_report.get_filter_value("show_zero_values")) {
                value = $(`<span>${value}</span>`).css("color", "#999").prop("outerHTML");
            }
        }
        
        return value;
    }
};

erpnext.utils.add_dimensions("Disclosure Details", 6);